<template>
  <el-container>
    <!-- 头部 -->
    <el-header>
      <img src="../assets/logo.png" alt="123" />
      <h2>后台管理</h2>
      <el-button type="info" @click="logout">退出登录</el-button>
    </el-header>
    <el-container>
      <!-- 侧边栏 router  是否使用 vue-router 的模式，启用该模式会在激活导航时以 index 作为 path 进行路由跳转-->
      <el-aside :width="isfalse ? '64px' : '200px'" >
        <div class="toggle-btn" @click="togglecollapse">|||</div>
        <el-menu
          :router='true'
          background-color="#545c64"
          text-color="#fff"
          active-text-color="#409EfE"
          unique-opened
          :default-active="activePath"
          :collapse = "isfalse"
          :collapse-transition = "false"
        >
        <!-- 一级菜单栏 index页码，一次性打开几个导航的属性-->
          <el-submenu v-for="i in menulist" :key="i.id"  :index="i.id + ''">
        <!-- 一级菜单模板区 -->
            <template slot="title">
              <!-- 小图标 -->
              <i :class="imgobj[i.id]"></i>
              <!-- 标题 -->
              <span>{{i.authName}}</span>
            </template>
              <!-- 二级菜单栏 -->
              <el-menu-item :index="'/' + j.path" v-for="j in i.children" :key="j.id" @click="defaultActivePath('/' + j.path)">
                <!-- 小图标 -->
                <i class="el-icon-s-promotion"></i>
                <!-- 标题 -->
                <span>{{j.authName}}</span>
              </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主体 -->
      <el-main>
        <!-- router-view 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
/*eslint-disable*/
export default {
  data(){
    return {
    //左侧菜单数据
    menulist:[],
    // 按id匹配图标
    imgobj: {
      '125':'el-icon-s-shop',
      '103':'el-icon-s-marketing',
      '101':'el-icon-s-flag',
      '102':'el-icon-s-comment',
      '145':'el-icon-s-finance'
    },
    isfalse:false, // 是否折叠菜单栏
    activePath: '' // 菜单激活状态  
    }
  },
  created(){
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('active')
  },
  methods: {
    // 退出功能
    logout() {
      window.sessionStorage.clear();
      this.$router.push("login");
    },
    // 获取所有的菜单
    async getMenuList() {
      const { data:res } =await this.$http.get('menus')
      if ( res.meta.status !==200 ){
        window.sessionStorage.clear();
        this.$router.push("login");
        return this.$message.error(res.meta.msg)
      }
      this.menulist = res.data
    },
    // 是否折叠菜单栏
    togglecollapse(){
      this.isfalse = !this.isfalse
    },
    // 菜单激活状态
    defaultActivePath(Path) {
      window.sessionStorage.setItem('active', Path)
      this.activePath = Path
    }
  },
};
</script>

<!-- scoped 设定样式只生效于本文件组件，如果不设则生效于全局-->
<style lang="less" scoped>
.toggle-btn {
  background-color: violet;
  line-height: 10px;
  text-align: center;
  cursor: pointer;
}
span {
  margin-left: 10px;
}
.el-header {
  background-color: #333;
  color: white;
  justify-content: space-between;
  display: flex;
  align-items: center;
  > h2 {
    display: flex;
    align-items: center;
  }
  > img {
    float: left;
    height: 30px;
    width: 30px;
    padding-left: 0;
    align-items: center;
    border-radius: 50%;
  }
}

.el-aside {
  background-color: #545c64;
  color: white;
  text-align: center;
  line-height: 100%;
  .el-menu {
      border-right:none
      

  }

}

.el-main {
  background-color: #e9eef3;
  color: #333;
  text-align: center;
}
.el-container {
  height: 100%;
}
</style>
